package cn.hn.tx.mail.action;

import java.util.ArrayList;
import java.util.List;

import cn.hn.comm.Tools;
import cn.hn.tx.mail.model.MailAddress;
import cn.hn.tx.mail.model.MailCrawlURL;
import cn.hn.tx.mail.service.IMailCrawlService;
import cn.hn.tx.mail.service.IMailService;

import com.opensymphony.xwork2.ActionSupport;

public class MailCrawlAction extends ActionSupport {

	private IMailCrawlService mailCrawlService;

	private IMailService mailService;

	private String crawlURL;

	private boolean existURL;

	private MailCrawlURL mailCrawlURL;
	
	private Integer newMailCount;
	
	private Integer totalMailCount;
	
	//前缀
	private String prefix;
	//起始页
	private Integer pageno;
	//倍数
	private Integer multiple; 
	//后缀
	private String suffix;
	//页数
	private Integer pagenum;
	
	
	public String crawlMail() {
		// 是否已经存在该链接
		existURL = mailCrawlService.hasExistUrl(crawlURL.trim());
		if (existURL) {
			mailCrawlURL = mailCrawlService.queryMailCrawlURLByURL(crawlURL);
			return "existurl";
		}
		exeCrawlMail(true);
		return SUCCESS;
	}
	
	/**
	 * 批量抓取
	 * @return
	 */
	public String crawlMailBatch()
	{
		int iNewMailCount=0;
		String strCrawlURL="";
		for(int i=0;i<pagenum;i++)
		{
			crawlURL=prefix+(pageno+i)*multiple+suffix;
			strCrawlURL+=crawlURL+"<br/>";
			// 是否已经存在该链接
			existURL = mailCrawlService.hasExistUrl(crawlURL.trim());
			exeCrawlMail(!existURL);
			iNewMailCount+=newMailCount.intValue();
		}
		newMailCount=iNewMailCount;
		crawlURL=strCrawlURL;
		return SUCCESS;
	}
	
	public String confirmReCrawMail()
	{
		exeCrawlMail(false);
		return SUCCESS;
	}

	public void exeCrawlMail(boolean isFirstCrawl) {
		List<String> listMail = mailCrawlService.crawlMail(crawlURL);

		List<MailAddress> existMailAddress = mailService.queryAllMailAddress();
		
		//提出已经存在的邮箱地址
		List<String> existMail=new ArrayList<String>();
		for(MailAddress ma:existMailAddress)
		{
			existMail.add(ma.getMail_address());
		}
		
		
		List<MailAddress> newMailAddress = new ArrayList<MailAddress>();
		for (String mail : listMail) {
			
			if(!existMail.contains(mail))
			{
				MailAddress mailAR = new MailAddress();
				mailAR.setStatus(MailAddress.STATE_ENABLE);
				mailAR.setMail_address(mail);
				mailAR.setInsert_time(Tools.getCurDate(null));
				newMailAddress.add(mailAR);				
			}
			
//			boolean isImport = true;
//			mail = mail.trim();
//			for (MailAddress md : existMailAddress) {
//				if (md.getMail_address().equals(mail)) {
//					isImport = false;
//					break;
//				}
//			}
//			if (isImport) {
//				MailAddress mailAR = new MailAddress();
//				mailAR.setStatus(MailAddress.STATE_ENABLE);
//				mailAR.setMail_address(mail);
//				mailAR.setInsert_time(Tools.getCurDate(null));
//				newMailAddress.add(mailAR);
//			}
		}
		for (MailAddress md : newMailAddress) {
			mailService.insertMailAddress(md);
		}

		if (isFirstCrawl) {
			mailCrawlURL = new MailCrawlURL();
			mailCrawlURL.setFirst_crawl_time(Tools.getCurDate(null));
			mailCrawlURL.setLast_crawl_num(newMailAddress.size());
			mailCrawlURL.setLast_crawl_time(Tools.getCurDate(null));
			mailCrawlURL.setMax_crawl_num(newMailAddress.size());
			mailCrawlURL.setUrl(crawlURL);
			mailCrawlService.insertMailCrawlURL(mailCrawlURL);

		} else {
			mailCrawlURL = mailCrawlService.queryMailCrawlURLByURL(crawlURL);
			mailCrawlURL.setLast_crawl_num(newMailAddress.size());
			mailCrawlURL.setLast_crawl_time(Tools.getCurDate(null));
			if (mailCrawlURL.getMax_crawl_num() < newMailAddress.size()) {
				mailCrawlURL.setMax_crawl_num(newMailAddress.size());
			}
			mailCrawlService.updateMailCrawlURL(mailCrawlURL);
		}
		newMailCount=newMailAddress.size();
		totalMailCount=newMailCount;
		totalMailCount+=existMailAddress.size();


	}

	public IMailCrawlService getMailCrawlService() {
		return mailCrawlService;
	}

	public void setMailCrawlService(IMailCrawlService mailCrawlService) {
		this.mailCrawlService = mailCrawlService;
	}

	public IMailService getMailService() {
		return mailService;
	}

	public void setMailService(IMailService mailService) {
		this.mailService = mailService;
	}

	public String getCrawlURL() {
		return crawlURL;
	}

	public void setCrawlURL(String crawlURL) {
		this.crawlURL = crawlURL;
	}

	public boolean isExistURL() {
		return existURL;
	}

	public void setExistURL(boolean existURL) {
		this.existURL = existURL;
	}

	public MailCrawlURL getMailCrawlURL() {
		return mailCrawlURL;
	}

	public void setMailCrawlURL(MailCrawlURL mailCrawlURL) {
		this.mailCrawlURL = mailCrawlURL;
	}

	public Integer getNewMailCount() {
		return newMailCount;
	}

	public void setNewMailCount(Integer newMailCount) {
		this.newMailCount = newMailCount;
	}

	public Integer getTotalMailCount() {
		return totalMailCount;
	}

	public void setTotalMailCount(Integer totalMailCount) {
		this.totalMailCount = totalMailCount;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}



	public Integer getPageno() {
		return pageno;
	}

	public void setPageno(Integer pageno) {
		this.pageno = pageno;
	}

	public Integer getMultiple() {
		return multiple;
	}

	public void setMultiple(Integer multiple) {
		this.multiple = multiple;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public Integer getPagenum() {
		return pagenum;
	}

	public void setPagenum(Integer pagenum) {
		this.pagenum = pagenum;
	}

}
